// 无头浏览器模块
const puppeteer = require("puppeteer");
// 创建文件
const createFile = require("./createFile");
// 爬取页面
const { crawlPageOrigin, crawlPage } = require("./config");

// 网页爬虫
async function crawler() {
  //创建实例
  const browser = await puppeteer.launch({
    //无浏览器界面启动
    headless: "new",
    //放慢浏览器执行速度，方便测试观察
    slowMo: 100,
    // 设置打开的浏览器窗口尺寸
    defaultViewport: { width: 960, height: 540 },
  });

  // 新建tab页面
  const page = await browser.newPage();
  await page.goto(crawlPage, { waitUntil: "networkidle0" });

  const articleList = await page.evaluate((pageOrigin) => {
    return getData();

    // 操作dom, 获取文章列表页面的内容
    function getData() {
      const list = [];
      const itemSelector = ".hot-list >.article-item-link";
      const commonSelector = ".article-item-wrap > .article-item-left";
      const rankSelector = [commonSelector, ".article-number"].join(">");
      const titleSelector = [commonSelector, ".article-detail > .aticle-title"].join(">");
      document.querySelectorAll(itemSelector).forEach((ele) => {
        // const rank = ele.querySelector(rankSelector).innerText.replace(/[\n\s]/g, "");
        const rank = ele.querySelector(rankSelector).innerText;
        const title = ele.querySelector(titleSelector).getAttribute("title");
        const link = `${pageOrigin}${ele.getAttribute("href")}`;

        list.push({
          排名: rank,
          标题: title,
          链接: link,
        });
      });

      return list;
    }
  }, crawlPageOrigin);

  // 创建新文件,写入列表数据
  await createFile(articleList);

  // 截屏--调试用
  // await page.screenshot({ path: `preview.png` });
  // 关闭实例
  await browser.close();
}

module.exports = crawler;
